﻿
Imports System
Imports System.Data
Imports System.Text

Imports Moca.Db

Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports Moca.Db.Helper

Namespace Db.Helper

#Region " DDL "

	' CREATE TABLE "MOCA"."tbDemoEntities" 
	'  (	"ID" NVARCHAR2(5) NOT NULL ENABLE, 
	'"Code" NUMBER(5,0) NOT NULL ENABLE, 
	'"Name" NVARCHAR2(256), 
	'"Note" NVARCHAR2(2000), 
	'"SUMMARY" NUMBER(5,2), 
	'"MSG" VARCHAR2(2000 BYTE), 
	' CONSTRAINT "TBDEMOENTITIES_PK" PRIMARY KEY ("ID")
	' USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
	' STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
	' PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
	' TABLESPACE "USERS"  ENABLE
	'  ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
	' STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
	' PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
	' TABLESPACE "USERS" ;


	'create or replace 
	'procedure "USRDEMO" (
	'  p1 IN NUMBER,
	'  p2 IN OUT NUMBER
	')
	'is
	'  ret  VARCHAR2(256);
	'begin

	'/*
	'  ストアドのデモです。
	'  PGテストとかで使うときは使ってくださいませ。
	'*/

	'  p2 := p2 + p1 + 100;
	'  ret := '123';

	'  RETURN;

	'end;

	'create or replace 
	'function "FUNCDEMO" (
	'  p1 IN NUMBER,
	'  p2 IN OUT NUMBER
	')
	'RETURN VARCHAR2 AS 
	'		begin()

	'/*
	'  関数のデモです。
	'  PGテストとかで使うときは使ってくださいませ。
	'*/

	'  p2 := p2 + p1 + 100;
	'		Return '999';

	'end;

#End Region

	'''<summary>
	'''OracleDbAccessHelperTest のテスト クラスです。すべての
	'''OracleDbAccessHelperTest 単体テストをここに含めます
	'''</summary>
	<TestClass()> _
	Public Class OracleAccessHelperTest
		Inherits Moca.Test.AbstractUnitTest

		Private testContextInstance As TestContext

		Protected dbaTest As IDaoOracleTest

		'''<summary>
		'''現在のテストの実行についての情報および機能を
		'''提供するテスト コンテキストを取得または設定します。
		'''</summary>
		Public Property TestContext() As TestContext
			Get
				Return testContextInstance
			End Get
			Set(value As TestContext)
				testContextInstance = value
			End Set
		End Property

#Region "追加のテスト属性"
		'
		'テストを作成するときに、次の追加属性を使用することができます:
		'
		'クラスの最初のテストを実行する前にコードを実行するには、ClassInitialize を使用
		<ClassInitialize()> _
		Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
			ClassInitialize(testContext)

			'Using context As New OracleContext
			'	context.Demos.SqlQuery("DELETE from tbDemoEntities")
			'	context.SaveChanges()
			'End Using
			'Using context As New OracleContext
			'	Dim lst = New List(Of tbDemoEntity)() From {
			'	 New tbDemoEntity() With {
			'	  .ID = "1",
			'	  .Code = 10,
			'	  .Name = "hoge1",
			'	  .Note = "hoge1 note"
			'	 },
			'	 New tbDemoEntity() With {
			'	  .ID = "2",
			'	  .Code = 20,
			'	  .Name = "hoge2",
			'	 .Note = "hoge2 note"
			'	 }
			'	}
			'	lst.ForEach(Function(ee) context.Demos.Add(ee))
			'	context.SaveChanges()
			'End Using
		End Sub

		'クラスのすべてのテストを実行した後にコードを実行するには、ClassCleanup を使用
		<ClassCleanup()> _
		Public Shared Sub MyClassCleanup()
			ClassCleanup()
		End Sub

		''各テストを実行する前にコードを実行するには、TestInitialize を使用
		<TestInitialize()> _
		Public Sub MyTestInitialize()
			Me.TestInitialize()
		End Sub

		''各テストを実行した後にコードを実行するには、TestCleanup を使用
		<TestCleanup()> _
		Public Sub MyTestCleanup()
			Me.TestCleanup()
		End Sub

#End Region

		'''<summary>
		'''ConnectionTest のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ConnectionTest()
			Dim target As OracleAccessHelper = dbaTest.GetHelper
			Dim actual As DataTable
			actual = target.ConnectionTest

			Dim dt As DataTable
			dt = target.Restrictions()
			For Each row As DataColumn In dt.Columns
				Debug.Print(row.ColumnName & ", ")
			Next
			For Each row As DataRow In dt.Rows
				Dim sb As New StringBuilder
				For Each col As Object In row.ItemArray
					sb.Append(col.ToString())
					sb.Append(", ")
				Next
				Debug.Print(sb.ToString)
			Next
		End Sub

		'''<summary>
		'''CDbParameterName のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub CDbParameterNameTest()
			Dim target As OracleAccessHelper = New OracleAccessHelper(dbaTest)
			Dim name As String = "hoge"
			Dim expected As String = ":hoge"
			Dim actual As String
			actual = target.CDbParameterName(name)
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''ErrorCount のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ErrorCountTest()
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				Dim target As IDbAccessHelper = dbaTest.GetHelper
				Dim expected As Integer = 1
				Dim actual As Integer
				actual = target.ErrorCount(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
				Assert.AreEqual(expected, actual)
			End Try
		End Sub

		'''<summary>
		'''ErrorNumbers のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ErrorNumbersTest()
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				Dim target As IDbAccessHelper = dbaTest.GetHelper
				Dim expected() As String = {OracleErrorNumbers.DuplicationPKey}
				Dim actual() As String
				actual = target.ErrorNumbers(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
				Assert.AreEqual(expected.Count, actual.Count)
				Assert.AreEqual(expected(0), actual(0))
			End Try
		End Sub

		'''<summary>
		'''GetSchemaColumns のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaColumnsTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim tablename As String = "tbDemoEntities"
			Dim table As DbInfoTable = New DbInfoTable("MOCA", String.Empty, tablename, String.Empty)
			Dim actual As DbInfoColumnCollection

			actual = target.GetSchemaColumns(table)
			Assert.AreEqual(6, actual.Count)
			Assert.AreEqual(5, actual("ID").MaxLength)
			Assert.AreEqual(0, actual("ID").Precision)
			Assert.AreEqual(0, actual("ID").Scale)
			Assert.AreEqual(True, actual("ID").UniCode)
			Assert.AreEqual(5, actual("Code").MaxLength)
			Assert.AreEqual(5, actual("Code").Precision)
			Assert.AreEqual(0, actual("Code").Scale)
			Assert.AreEqual(False, actual("Code").UniCode)
			Assert.AreEqual(256, actual("Name").MaxLength)
			Assert.AreEqual(0, actual("Name").Precision)
			Assert.AreEqual(0, actual("Name").Scale)
			Assert.AreEqual(True, actual("Name").UniCode)
			Assert.AreEqual(2000, actual("Note").MaxLength)		' MAX
			Assert.AreEqual(0, actual("Note").Precision)
			Assert.AreEqual(0, actual("Note").Scale)
			Assert.AreEqual(True, actual("Note").UniCode)
			Assert.AreEqual(6, actual("SUMMARY").MaxLength)	' MAX
			Assert.AreEqual(5, actual("SUMMARY").Precision)
			Assert.AreEqual(2, actual("SUMMARY").Scale)
			Assert.AreEqual(False, actual("SUMMARY").UniCode)
			Assert.AreEqual(2000, actual("MSG").MaxLength)
			Assert.AreEqual(0, actual("MSG").Precision)
			Assert.AreEqual(0, actual("MSG").Scale)
			Assert.AreEqual(False, actual("MSG").UniCode)
		End Sub

		'''<summary>
		'''GetSchemaFunctions のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaFunctionsTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim name As String = "MOCA.MOCA..funcDemo".ToUpper
			Dim actual As DbInfoFunctionCollection
			actual = target.GetSchemaFunctions
			Assert.AreEqual(True, actual.Keys.Contains(name))
		End Sub

		'''<summary>
		'''GetSchemaProcedures のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaProceduresTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim name As String = "MOCA.MOCA..usrDemo".ToUpper
			Dim actual As DbInfoProcedureCollection
			actual = target.GetSchemaProcedures
			Assert.AreEqual(1, actual.Count)
			Assert.AreEqual(True, actual.Keys.Contains(name))
		End Sub

		'''<summary>
		'''GetSchemaTable のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaTableTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim tablename As String = "tbDemoEntities"
			Dim actual As DbInfoTable
			actual = target.GetSchemaTable(tablename)
			Assert.AreEqual("MOCA", actual.Catalog)
			Assert.AreEqual("MOCA", actual.Schema)
			Assert.AreEqual(tablename, actual.Name)
			Assert.AreEqual("User", actual.Typ)
		End Sub

		'''<summary>
		'''GetSchemaTables のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaTablesTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim tablename1 As String = "MOCA.MOCA.User.tbDemoEntities"
			Dim obj As Object = Nothing
			Dim actual As DbInfoTableCollection
			actual = target.GetSchemaTables
			Assert.AreEqual(True, actual.Keys.Contains(tablename1))
		End Sub

		'''<summary>
		'''HasSqlNativeError のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub HasSqlNativeErrorTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As Boolean = True
			Dim actual As Boolean
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				actual = target.HasSqlNativeError(ex.GetBaseException, OracleErrorNumbers.DuplicationPKey)
				Debug.Print(ex.GetBaseException.Message)
				Debug.Print(String.Join(",", target.ErrorNumbers(ex.GetBaseException)))
			End Try
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''HasSqlNativeErrorDuplicationPKey のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub HasSqlNativeErrorDuplicationPKeyTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As Boolean = True
			Dim actual As Boolean
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				actual = target.HasSqlNativeErrorDuplicationPKey(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
				Debug.Print(String.Join(",", target.ErrorNumbers(ex.GetBaseException)))
			End Try
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''HasSqlNativeErrorTimtout のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub HasSqlNativeErrorTimtoutTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As Boolean = True
			Dim actual As Boolean
			Dim actualNo As String = String.Empty
			Try
				dbaTest.TimeoutTest(2, 2)
			Catch ex As Exception
				actual = target.HasSqlNativeErrorTimtout(ex.GetBaseException)
				actualNo = String.Join(",", target.ErrorNumbers(ex.GetBaseException))
				Debug.Print(ex.GetBaseException.Message)
				Debug.Print(actualNo)
			End Try
			Assert.AreEqual(expected, actual)
			Assert.AreEqual(CStr(OracleErrorNumbers.LockTimeOut), actualNo)
		End Sub

		'''<summary>
		'''RefreshProcedureParameters のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub RefreshProcedureParametersTest()
			Const C_SQL As String = "USRDEMO"
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim cmd As IDbCommand = dbaTest.GetMe.CreateCommandStoredProcedure(C_SQL).Command
			Dim pram As IDataParameter
			target.RefreshProcedureParameters(cmd)
			Assert.AreEqual(2, cmd.Parameters.Count)
			pram = CType(cmd.Parameters(0), IDataParameter)
			Assert.AreEqual("P1", pram.ParameterName)
			Assert.AreEqual(ParameterDirection.Input, pram.Direction)
			pram = CType(cmd.Parameters(1), IDataParameter)
			Assert.AreEqual("P2", pram.ParameterName)
			Assert.AreEqual(ParameterDirection.InputOutput, pram.Direction)
		End Sub

		'''<summary>
		'''RefreshProcedureParameters のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub RefreshFunctionParametersTest()
			Const C_SQL As String = "FUNCDEMO"
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim cmd As IDbCommand = dbaTest.GetMe.CreateCommandStoredProcedure(C_SQL).Command
			Dim pram As IDataParameter
			target.RefreshProcedureParameters(cmd)
			Assert.AreEqual(3, cmd.Parameters.Count)
			pram = CType(cmd.Parameters(0), IDataParameter)
			Assert.AreEqual("RETURN_VALUE", pram.ParameterName)
			Assert.AreEqual(ParameterDirection.ReturnValue, pram.Direction)
			pram = CType(cmd.Parameters(1), IDataParameter)
			Assert.AreEqual("P1", pram.ParameterName)
			Assert.AreEqual(ParameterDirection.Input, pram.Direction)
			pram = CType(cmd.Parameters(2), IDataParameter)
			Assert.AreEqual("P2", pram.ParameterName)
			Assert.AreEqual(ParameterDirection.InputOutput, pram.Direction)
		End Sub

		'''<summary>
		'''PlaceholderMark のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub PlaceholderMarkTest()
			Dim target As OracleAccessHelper = dbaTest.GetHelper
			Dim actual As String
			actual = target.PlaceholderMark
			Assert.AreEqual(":", actual)
		End Sub
	End Class

End Namespace
